<script src=../priorities/resources/common.js></script>
<script src=../resources/testharness.js></script>
<script src=../resources/testharnessreport.js></script>

<script>
  const tests = [
    // rel=style
    {
      description: 'low importance on <link rel=preload as=style> not loaded by the preload scanner must be fetched with kHigh resource load priority',
      as: 'style',
      importance: 'low',
      resource: 'dummy.css',
      expected_priority: kHigh
    },
    {
      description: 'missing importance on <link rel=preload as=style> not loaded by the preload scanner must be fetched with kVeryHigh resource load priority',
      as: 'style',
      resource: 'dummy.css',
      expected_priority: kVeryHigh
    },
    {
      description: 'high importance on <link rel=preload as=style> not loaded by the preload scanner must be fetched with kVeryHigh resource load priority',
      as: 'style',
      importance: 'high',
      resource: 'dummy.css',
      expected_priority: kVeryHigh
    },
    // rel=script
    {
      description: 'low importance on <link rel=preload as=script> not loaded by the preload scanner must be fetched with kLow resource load priority',
      as: 'script',
      importance: 'low',
      resource: 'dummy.js',
      expected_priority: kLow
    },
    {
      description: 'missing importance on <link rel=preload as=script> not loaded by the preload scanner must be fetched with kHigh resource load priority',
      as: 'script',
      resource: 'dummy.js',
      expected_priority: kHigh
    },
    {
      description: 'high importance on <link rel=preload as=script> not loaded by the preload scanner must be fetched with kHigh resource load priority',
      as: 'script',
      importance: 'high',
      resource: 'dummy.js',
      expected_priority: kHigh
    },
    // rel=fetch
    {
      description: 'low importance on <link rel=preload as=fetch> not loaded by the preload scanner must be fetched with kLow resource load priority',
      as: 'fetch',
      importance: 'low',
      resource: 'dummy.txt',
      expected_priority: kLow
    },
    {
      description: 'missing importance on <link rel=preload as=fetch> not loaded by the preload scanner must be fetched with kHigh resource load priority',
      as: 'fetch',
      resource: 'dummy.txt',
      expected_priority: kHigh
    },
    {
      description: 'high importance on <link rel=preload as=fetch> not loaded by the preload scanner must be fetched with kHigh resource load priority',
      as: 'fetch',
      importance: 'high',
      resource: 'dummy.txt',
      expected_priority: kHigh
    },
    // rel=image
    {
      description: 'low importance on <link rel=preload as=image> not loaded by the preload scanner must be fetched with kLow resource load priority',
      as: 'image',
      importance: 'low',
      resource: 'square.png',
      expected_priority: kLow
    },
    {
      description: 'missing importance on <link rel=preload as=image> not loaded by the preload scanner must be fetched with kLow resource load priority',
      as: 'image',
      resource: 'square.png',
      expected_priority: kLow
    },
    {
      description: 'high importance on <link rel=preload as=image> not loaded by the preload scanner must be fetched with kHigh resource load priority',
      as: 'image',
      importance: 'high',
      resource: 'square.png',
      expected_priority: kHigh
    }
  ];

  let iteration = 0;
  for (const test of tests) {
    promise_test(async () => {
      const link = document.createElement('link');
      link.rel = 'preload';
      link.as = test.as;
      if (test.importance) link.importance = test.importance;

      const url = new URL(`../resources/${test.resource}?${iteration++}`, location);
      const priorityPromise = internals.getInitialResourcePriority(url, document);
      link.href = url;
      document.head.appendChild(link);
      const priority = await priorityPromise;
      assert_equals(priority, test.expected_priority, test.description);
    });
  }
</script>
